import time
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
from .models import Classification, Table
from .serializer import CreateService
from utils.pagnations import MyPagionation
from user.models import Merchants


# 分类展示
class Classi(APIView):
    def post(self, request):
        good = Classification.objects.all().order_by('operating_time')
        goode = CreateService(good, many=True)
        pagination_class = MyPagionation
        return Response({'lesta': goode, 'pagination': pagination_class})


# 添加菜品分类
class FoodAdd(APIView):
    def post(self, request):
        gid = request.data.get('gid')
        name = request.data.get('name')
        goodsi = Classification.objects.create(name=name, food='菜品分类', mer=gid, operating_time=time.strftime('%Y-%m-%d %H:%M:%S'))
        goodsi.save()
        return Response({'messages': 'Creating a new dish type', 'code': status.HTTP_200_OK})


#     套餐分类
class classAdd(APIView):
    def post(self, request):
        cid = request.data.get('cid')
        name = request.data.get('name')
        good = Classification.objects.create(name=name, package='套餐分类',  mer=cid,operating_time=time.strftime('%Y-%m-%d %H:%M:%S'))
        good.save()
        return Response({'messages': 'Package added successfully', 'code': status.HTTP_200_OK})


#修改菜品分类
class ModifiedFoos(APIView):
    def post(self, request):
        mid = request.data.get('bid')
        gid = request.data.get('gid')
        name = request.data.get('name')
        good = Classification.objects.filter(id=gid, name=name, mer=mid).all()
        if good.count() > 0:
            return Response({'messages': 'Theres no such thing', 'code': status.HTTP_402_PAYMENT_REQUIRED})
        try:
            goode = Classification.objects.filter(id = gid).update(name=name)
            goode.save()
            return Response({'messages': 'The menu category is modified successfully. Procedure', 'code': status.HTTP_200_OK})
        except Exception as e:
            return Response({'messages': 'Failed to modify the food category. Procedure', 'code': status.HTTP_402_PAYMENT_REQUIRED})


# 删除菜品分类
class DeleteSer(APIView):
    def post(self, request):
     gid = request.data.get('gid')
     mid = request.data.get('mid')
     good = Classification.objects.filter(id=gid, mer=mid).count()
     if good == 0:
         return Response({'messages': 'Without this dish, please add first', 'code': status.HTTP_402_PAYMENT_REQUIRED})
     else:
         goods = Classification.objects.filter(id=gid, mer=mid)
         goods.delete()
         return Response({'messages': 'Remove the success of the dish', 'code': status.HTTP_200_OK})


# 菜单
class Menu_adds(APIView):
    def post(self, request):
        id = request.data.get('id')
        name = request.data.get('name')
        price = request.data.get('price')
        img = request.data.get('img')
        descri = request.data.get('descri')
        good = Classification.objects.filter(id=id).all()
        if not good:
            return Response({'messages': 'The shop is empty', 'code': status.HTTP_400_BAD_REQUEST})
        goodse = Table.objects.filter(name=name, description=descri, price=price, img=img, classifica__name=good, classifica__food=('菜品'))
        goodse.save()
        return Response({'messages': 'Goods added successfully', 'code': status.HTTP_200_OK})


# 菜单
class Menus_adds(APIView):

    def post(self, request):
        id = request.data.get('id')
        name = request.data.get('name')
        price = request.data.get('price')
        img = request.data.get('img')
        descri = request.data.get('descri')
        pack = request.data.get('pack')
        good = Classification.objects.filter(id=id, package=pack).all()
        if not good:
            return Response({'messages': 'The shop is empty', 'code': status.HTTP_400_BAD_REQUEST})
        goodse = Table.objects.filter(name=name, description=descri, price=price, img=img, classifica=good)
        goodse.save()
        return Response({'messages': 'Goods added successfully', 'code': status.HTTP_200_OK})


class Bishes(APIView):
    def post(self, request):
        pass






        


